Difficulty understanding dxl function overloading

Straight from the dxl Reference manual (page 19):

Overloaded functions and operators
Most functions and operators can be declared to take arguments and return values of more than one type.
Example
This example overloads a commonly used identifier print to provide an object printer.

// Overload print to define an Object printer
void print(Object o) { 
string h = o."Object Heading" 
string t = o."Object Text" 
print h ":\n\n" t "\n"
}
print current Object


This example does exactly what one would expect it to.
The rewrite shown below, however, has problems.

 

 

bool bWriteToStream = true
Stream object_output = write "d:\\DOORS\\dxl output\\metrics.txt" 
void print(string s) {
    bWriteToStream ? object_output <<  s "\n" : print s "\n"
}
print "Goes to DXL output stream as expected"
close object_output


If one sets bWriteToStream = true, evrything is fine. If, however, one sets bWriteToStream = false, DOORS will flat out crash (close) every time.

Stranger still, if I reverse the conditional statement within the overload, e.g.,

 

 

 

bWriteToStream ? print s "\n" : object_output <<  s "\n"


It works fine for both bWriteToStream = true and bWriteToStream = false (and yes, I realize my bool definnition is now backwards!)
What in the world am I missing?

BTW, the whole impetus behind this is to provide a quick and dirty means of adding a simple switch to existing dxl modules to control whether print output goes to the DXL output pane of the DXL editing window, or to a text file. We've had some issues with the editing window going blank when large amounts of data get written to it, and we're trying to at least find out what data is being produced.

 


dedawson - Mon Mar 12 17:28:35 EDT 2012

Re: Difficulty understanding dxl function overloading
Peter_Albert - Tue Mar 13 03:54:08 EDT 2012

You should first overload the original print routine. In your script, DXL gets confused, because you first overload

void print(string s)

but then in the routine body you are using the original print routine in

bWriteToStream ? object_output <<  s "\n" : print s "\n"

.

I don't have the time to test it now, but the following should work:
 

void print_orig(string s){print s}
bool bWriteToStream = true
Stream object_output = write "d:\\DOORS\\dxl output\\metrics.txt" 
void print(string s) {
    bWriteToStream ? object_output <<  s "\n" : print_orig(s) "\n"
}
print "Goes to DXL output stream as expected"
close object_output

 


Regards,

Peter

 

Re: Difficulty understanding dxl function overloading
dedawson - Tue Mar 13 12:06:12 EDT 2012

Peter_Albert - Tue Mar 13 03:54:08 EDT 2012

You should first overload the original print routine. In your script, DXL gets confused, because you first overload

void print(string s)

but then in the routine body you are using the original print routine in

bWriteToStream ? object_output <<  s "\n" : print s "\n"

.

I don't have the time to test it now, but the following should work:
 

void print_orig(string s){print s}
bool bWriteToStream = true
Stream object_output = write "d:\\DOORS\\dxl output\\metrics.txt" 
void print(string s) {
    bWriteToStream ? object_output <<  s "\n" : print_orig(s) "\n"
}
print "Goes to DXL output stream as expected"
close object_output

 


Regards,

Peter

 

Thanks Peter. What you suggested worked, but I was still puzzled as to why the example in the DXL manual failed, since I followed it exactly, merely substituting string for Object. The overload in the manual modified the definition of print, yet still called print within it's body.

Once I put together the following, however, the light bulb came on.

Module m = current
Object o = current
 
bool bWriteToStream = true
Stream output_stream = write "d:\\DOORS\\dxl output\\overload_test.txt" 
void print_orig(string s){print s}
void print(string s) {bWriteToStream ? output_stream << s "\n" : print_orig s "\n"}
void print(Object o) { 
  string h = o."Object Heading" 
  string t = o."Object Text" 
  print h ":\n\n" t "\nto output stream\n"
}
print current Object
print "This goes to the output stream"
bWriteToStream = false
print "And this goes to the output pane"
close output_stream

 


The key is that the print is already a string-based function. Overloading to handle Object is successful, as it is defining a variant print function that can handle the type Object. What I was originally doing failed because I was essentially defining a second print function, also of type string, hence the crash. Use of your approach works because the print_orig function serves as a string pass-through that de-conflicts the print function name.

Learn something new every day.
Thanks for the help.
david